<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : SessionService.php
 * Created on : 2020-7-25 10:50:38
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace haoteam\service;

use haoteam\service\RedisService;

/**
 * Class SessionService
 * Tiger <1192851302@qq.com> 2020-7-25 10:50:38
 */
class SessionService {

    public $online_data = [];

    /**
     * 快速实例化模型
     * @author Tiger <1192851302@qq.com>
     * @param bool $singleton 是否单例
     * @return static
     */
    public static function instance($singleton = true) {
        $name = static::class;
        if (isset($GLOBALS['model'][$name]) and is_object($GLOBALS['model'][$name]) and $singleton) {
            return $GLOBALS['model'][$name];
        } else {
            $GLOBALS['model'][$name] = new static();
            return $GLOBALS['model'][$name];
        }
    }

    /**
     * 写入登录信息
     * @author Tiger <1192851302@qq.com>
     * @param type $data
     * @param type $token
     * @param type $loginExpire
     * @return boolean
     */
    public function set($data, $token, $loginExpire = null) {
        $this->online_data = $data;
        RedisService::instance()->create('login:' . $token, $data, $loginExpire);
        return true;
    }

    /**
     * 退出登录
     * @author Tiger <1192851302@qq.com>
     * @param type $token
     * @return boolean
     */
    public function logout($token) {
        return RedisService::instance()->remove('login:' . $token); //消失吧
    }

    /**
     * 获取登录信息
     * @author Tiger <1192851302@qq.com>
     * @param type $token
     * @return boolean
     */
    public function get($token) {
        $this->online_data = RedisService::instance()->query('login:' . $token);
        return $this->online_data;
    }

}
